//
//  SVImage.m
//  cityStar
//
//  Created by wangjia on 14/12/13.
//  Copyright (c) 2014年 tiny. All rights reserved.
//

#import "SVImage.h"
#import "UIImageView+WebCache.h"

@interface SVImage ()<UIScrollViewDelegate>{
    UIScrollView *svContent;
    UIPageControl *pcContent;
}

@end

@implementation SVImage

- (id)init{
    self = [super init];
    if (self) {
        [self initContent];
    }
    return self;
}

- (void)awakeFromNib{
    [self initContent];
}

- (void)initContent{
    self.backgroundColor = [UIColor grayColor];
    svContent = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    svContent.pagingEnabled = YES;
    svContent.bounces = NO;
    svContent.showsHorizontalScrollIndicator = NO;
    svContent.delegate = self;
    pcContent = [[UIPageControl alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, 30)];
    [self addSubview:svContent];
    [self addSubview:pcContent];
}

- (void)setAryImage:(NSArray *)aryImage{
    _aryImage = aryImage;
    for (int i = 0; i < aryImage.count; i++) {
        UIImageView *ivContent = [[UIImageView alloc]initWithFrame:CGRectMake(self.frame.size.width*i, 0, self.frame.size.width, self.frame.size.height)];
        
        NSString *strImageUrl = @"";
        if ([aryImage[i] isKindOfClass:[NewsBanner class]]) {
            NewsBanner *banner = aryImage[i];
            strImageUrl = [@"" stringByAppendingFormat:@"http://api.icszx.com/image/c/id/%d/w/400/h/200/m/1",banner.image];
        }else{
            strImageUrl = aryImage[i];
        }
    
        [ivContent sd_setImageWithURL:[NSURL URLWithString:strImageUrl] placeholderImage:nil options:SDWebImageLowPriority|SDWebImageRetryFailed];
        [svContent addSubview:ivContent];
    }
    svContent.contentSize = CGSizeMake(self.frame.size.width * aryImage.count, self.frame.size.height);
    pcContent.numberOfPages = aryImage.count;
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    CGFloat pageWidth = scrollView.frame.size.width;
    NSInteger page = scrollView.contentOffset.x / pageWidth;
    pcContent.currentPage = page;
}

@end
